package com.qmx.launcher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.qmx.launcher.R;
import com.qmx.launcher.utils.FontUtil;

/**
 * @author chengxiangtong
 * @date 2018/10/19
 */
public class CustomerOnOff extends View {
    private Paint mPaint;
    private Paint bgPaint;
    private int mBgMonth;
    private float titleHeight;
    private int rowWidth;
    private int rowLStart;
    private int rowRStart;
    /**
     * 标题的颜色、大小
     */
    private int mTextColorMonth;
    private float mTextSizeMonth;
    private int mMonthRowL, mMonthRowR;
    private float mMonthRowSpac;
    private float mMonthSpac;
    private String state = "关";

    public CustomerOnOff(Context context) {
        this(context, null);
    }

    public CustomerOnOff(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerOnOff(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        /**
         * 获取自定义属性的值
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCalendar, defStyleAttr, 0);
        mMonthRowL = a.getResourceId(R.styleable.CustomCalendar_mMonthRowL, R.drawable.custom_calendar_row_left);
        mMonthRowR = a.getResourceId(R.styleable.CustomCalendar_mMonthRowR, R.drawable.custom_calendar_row_right);
        mMonthRowSpac = a.getDimension(R.styleable.CustomCalendar_mMonthRowSpac, 20);
        mTextColorMonth = a.getColor(R.styleable.CustomCalendar_mTextColorMonth, Color.WHITE);
        mTextSizeMonth = a.getDimension(R.styleable.CustomCalendar_mTextSizeMonth, 40);
        mMonthSpac = a.getDimension(R.styleable.CustomCalendar_mMonthSpac, 20);
        a.recycle();  //注意回收

        initCompute();
    }


    /**
     * 计算相关常量，构造方法中调用
     */
    private void initCompute() {
        mPaint = new Paint();
        bgPaint = new Paint();
        mPaint.setAntiAlias(true); //抗锯齿
        bgPaint.setAntiAlias(true); //抗锯齿

        //标题高度
        mPaint.setTextSize(mTextSizeMonth);
        titleHeight = FontUtil.getFontHeight(mPaint) + 2 * mMonthSpac;

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float height = titleHeight;
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                (int) height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawOnOffState(canvas);
    }

    @Override
    public void invalidate() {
        requestLayout();
        super.invalidate();
    }

    /****************************事件处理↓↓↓↓↓↓↓****************************/
    /**
     * 焦点坐标
     */
    private PointF focusPoint = new PointF();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (listener == null) {
            return false;
        }
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                listener.onLeftRowClick();
                return true;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                listener.onRightRowClick();
                return true;
        }
        return true;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        int action = event.getAction() & MotionEvent.ACTION_MASK;
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                focusPoint.set(event.getX(), event.getY());
//                touchFocusMove(focusPoint, false);
//                break;
//            case MotionEvent.ACTION_MOVE:
//                focusPoint.set(event.getX(), event.getY());
//                touchFocusMove(focusPoint, false);
//                break;
//            case MotionEvent.ACTION_OUTSIDE:
//            case MotionEvent.ACTION_CANCEL:
//            case MotionEvent.ACTION_UP:
//                focusPoint.set(event.getX(), event.getY());
//                touchFocusMove(focusPoint, true);
//                break;
//        }
//        return true;
//    }

    public void touchFocusMove(final PointF point, boolean eventEnd) {
        /**标题和星期只有在事件结束后才响应*/
        if (point.y <= titleHeight) {
            //事件在标题上
            if (eventEnd && listener != null) {
                if (point.x >= rowLStart && point.x < (rowLStart + 2 * mMonthRowSpac + rowWidth)) {
                    Log.w("", "点击左箭头");
                    listener.onLeftRowClick();
                } else if (point.x > rowRStart && point.x < (rowRStart + 2 * mMonthRowSpac + rowWidth)) {
                    Log.w("", "点击右箭头");
                    listener.onRightRowClick();
                }
            }
        }
    }


    /**
     * 开关转换
     */
    public void stateChange(String change) {
        state = (change == "关" ? "开" : "关");
        invalidate();
    }

    private void drawOnOffState(Canvas canvas) {
        //背景
        bgPaint.setColor(mBgMonth);
        RectF rect = new RectF(0, 0, getWidth(), titleHeight);
        canvas.drawRect(rect, bgPaint);
        //绘制月份
        mPaint.setTextSize(mTextSizeMonth);
        mPaint.setColor(mTextColorMonth);
        float textLen = FontUtil.getFontlength(mPaint, state);
        float textStart = (getWidth() - textLen) / 2;
        canvas.drawText(state, textStart,
                mMonthSpac + FontUtil.getFontLeading(mPaint), mPaint);
        /*绘制左右箭头*/
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mMonthRowL);
        int h = bitmap.getHeight();
        rowWidth = bitmap.getWidth();
        //float left, float top
        rowLStart = (int) (textStart - 2 * mMonthRowSpac - rowWidth);
        canvas.drawBitmap(bitmap, rowLStart + mMonthRowSpac - rowWidth * 2, (titleHeight - h) / 2, new Paint());
        bitmap = BitmapFactory.decodeResource(getResources(), mMonthRowR);
        rowRStart = (int) (textStart + textLen);
        canvas.drawBitmap(bitmap, rowRStart + mMonthRowSpac + rowWidth * 2, (titleHeight - h) / 2, new Paint());
    }

    private onClickListener listener;

    public void setOnClickListener(onClickListener listener) {
        this.listener = listener;
    }

    public interface onClickListener {

        public abstract void onLeftRowClick();

        public abstract void onRightRowClick();

    }
}
